package com.tfswx.jkgl.designer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.tfswx.jkgl.designer.dao.SjmxXtMxdyMapper;
import com.tfswx.jkgl.designer.dao.SjmxXtMxstgxMapper;
import com.tfswx.jkgl.designer.dto.GlgxQueryParam;
import com.tfswx.jkgl.designer.dto.SjmxQueryResult;
import com.tfswx.jkgl.designer.dto.mxgl.MxdyAddInputDTO;
import com.tfswx.jkgl.designer.dto.mxgl.MxdyDeleteInputDTO;
import com.tfswx.jkgl.designer.dto.mxgl.MxdyQueryInputDTO;
import com.tfswx.jkgl.designer.dto.mxgl.MxdyQueryOutputDTO;
import com.tfswx.jkgl.designer.dto.mxgl.MxdyUpdateInputDTO;
import com.tfswx.jkgl.designer.dto.mxgl.MxstgxAddInputDTO;
import com.tfswx.jkgl.designer.dto.mxgl.MxstgxDeleteInputDTO;
import com.tfswx.jkgl.designer.dto.mxgl.MxstgxQueryInputDTO;
import com.tfswx.jkgl.designer.dto.mxgl.MxstgxQueryOutputDTO;
import com.tfswx.jkgl.designer.dto.mxgl.MxstgxUpdateInputDTO;
import com.tfswx.jkgl.designer.model.SjmxXtMxdy;
import com.tfswx.jkgl.designer.model.SjmxXtMxstgx;
import com.tfswx.jkgl.designer.service.MxglService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ricky
 * @date 2021/12/10
 */
@Service
public class MxglServiceImpl implements MxglService {
    @Resource
    private SjmxXtMxdyMapper mxdyMapper;

    @Resource
    private SjmxXtMxstgxMapper mxstgxMapper;

    @Override
    public String addMxdy(MxdyAddInputDTO inputDTO) {
        SjmxXtMxdy sjmxXtMxdy = BeanUtil.copyProperties(inputDTO,SjmxXtMxdy.class);
        sjmxXtMxdy.setMxbm(IdUtil.fastSimpleUUID().toUpperCase());
        sjmxXtMxdy.setXh(System.currentTimeMillis());
        sjmxXtMxdy.setSfsc("N");
        sjmxXtMxdy.setCjsj(new Date());
        sjmxXtMxdy.setZhxgsj(new Date());
        int i = mxdyMapper.insert(sjmxXtMxdy);
        if (i != 1) {
            throw new RuntimeException("添加模型信息失败");
        }
        return sjmxXtMxdy.getMxbm();
    }

    @Override
    public boolean deleteMxdy(MxdyDeleteInputDTO inputDTO) {
        SjmxXtMxdy sjmxXtMxdy = BeanUtil.copyProperties(inputDTO,SjmxXtMxdy.class);
        sjmxXtMxdy.setZhxgsj(new Date());
        int i = mxdyMapper.delete(sjmxXtMxdy);
        if (i != 1) {
            throw new RuntimeException("删除模型信息失败");
        }
        return false;
    }

    @Override
    public boolean updateMxdy(MxdyUpdateInputDTO inputDTO) {
        SjmxXtMxdy sjmxXtMxdy = BeanUtil.copyProperties(inputDTO,SjmxXtMxdy.class);
        sjmxXtMxdy.setZhxgsj(new Date());
        int i = mxdyMapper.update(sjmxXtMxdy);
        if (i != 1) {
            throw new RuntimeException("修改模型信息失败");
        }
        return true;
    }

    @Override
    public List<MxdyQueryOutputDTO> queryMxdy(MxdyQueryInputDTO inputDTO) {
        SjmxXtMxdy sjmxXtMxdy = BeanUtil.copyProperties(inputDTO,SjmxXtMxdy.class);
        return mxdyMapper.queryMxdy(sjmxXtMxdy);
    }

    @Override
    public String addMxstgx(MxstgxAddInputDTO inputDTO) {
        SjmxXtMxstgx sjmxXtMxstgx = BeanUtil.copyProperties(inputDTO,SjmxXtMxstgx.class);
        sjmxXtMxstgx.setGxbm(IdUtil.fastSimpleUUID().toUpperCase());
        sjmxXtMxstgx.setXh(System.currentTimeMillis());
        sjmxXtMxstgx.setSfsc("N");
        sjmxXtMxstgx.setCjsj(new Date());
        sjmxXtMxstgx.setZhxgsj(new Date());
        int i = mxstgxMapper.insert(sjmxXtMxstgx);
        if (i != 1) {
            throw new RuntimeException("添加模型实体关系信息失败");
        }
        return sjmxXtMxstgx.getMxbm();

    }

    @Override
    public boolean deleteMxstgx(MxstgxDeleteInputDTO inputDTO) {
        SjmxXtMxstgx sjmxXtMxstgx = BeanUtil.copyProperties(inputDTO,SjmxXtMxstgx.class);
        sjmxXtMxstgx.setZhxgsj(new Date());
        int i = mxstgxMapper.delete(sjmxXtMxstgx);
        if (i != 1) {
            throw new RuntimeException("删除模型实体关系信息失败");
        }
        return false;
    }

    @Override
    public boolean updateMxstgx(MxstgxUpdateInputDTO inputDTO) {
        SjmxXtMxstgx sjmxXtMxstgx = BeanUtil.copyProperties(inputDTO,SjmxXtMxstgx.class);
        sjmxXtMxstgx.setZhxgsj(new Date());
        int i = mxstgxMapper.update(sjmxXtMxstgx);
        if (i != 1) {
            throw new RuntimeException("修改模型实体关系信息失败");
        }
        return true;
    }

    @Override
    public List<MxstgxQueryOutputDTO> queryMxstgx(MxstgxQueryInputDTO inputDTO) {
        SjmxXtMxstgx sjmxXtMxstgx = BeanUtil.copyProperties(inputDTO,SjmxXtMxstgx.class);
        return mxstgxMapper.queryMxstgx(sjmxXtMxstgx);
    }

    @Override
    public List<SjmxQueryResult> queryGlgx(GlgxQueryParam queryParam) {
        List<SjmxQueryResult> sjmxQueryResults = mxstgxMapper.queryGlgx(queryParam.getMxbm());
        return treeToList(listToTree(sjmxQueryResults), null);
    }

    private List<SjmxQueryResult> listToTree(List<SjmxQueryResult> list) {
        List<SjmxQueryResult> tree = new ArrayList<>();
        Map<String, SjmxQueryResult> map = new HashMap<>();
        for (SjmxQueryResult sjmxQueryResult : list) {
            map.put(sjmxQueryResult.getId(), sjmxQueryResult);
        }
        for (SjmxQueryResult sjmxQueryResult : list) {
            SjmxQueryResult sjmxQueryResult1 = map.get(sjmxQueryResult.getPid());
            if (sjmxQueryResult1 != null) {
                List<SjmxQueryResult> children = sjmxQueryResult1.getChildren();
                if (children == null) {
                    List<SjmxQueryResult> arrayList = new ArrayList<>();
                    arrayList.add(sjmxQueryResult);
                    sjmxQueryResult1.setChildren(arrayList);
                } else {
                    children.add(sjmxQueryResult);
                }
            } else {
                tree.add(sjmxQueryResult);
            }
        }
        return tree;
    }

    private List<SjmxQueryResult> treeToList(List<SjmxQueryResult> tree, String pid) {
        List<SjmxQueryResult> newList = new ArrayList<>();
        if (tree == null) {
            return newList;
        }
        for (SjmxQueryResult sjmxQueryResult : tree) {
            sjmxQueryResult.setId(IdUtil.fastSimpleUUID());
            sjmxQueryResult.setPid(pid);
            newList.add(sjmxQueryResult);
            newList.addAll(treeToList(sjmxQueryResult.getChildren(), sjmxQueryResult.getId()));
            sjmxQueryResult.setChildren(null);
        }
        return newList;
    }

}
